#include "MissileMunition.h"
#include <math.h>

#define PI 3.14
static const int DAMAGE = 5;

MissileMunition::MissileMunition(Map *m)
{
	map = m;
	damage = DAMAGE;
	width = 1;		// Z
	lenght = 1;		// X
	height = 0.1;	// Y
	sizeMissiles = 0.08;
	lenghtMissiles = 1.5;
	elevation = 0;
	oldX = 0;
	oldZ = 0;
	angle = 0;
	angleAnimation = 0;
	quad = gluNewQuadric();
	loadImage();
}

void MissileMunition::loadImage()
{
	Image* image1 = loadBMP("textures\\textureMissile.bmp");
	Image* image2 = loadBMP("textures\\textureCannon2.bmp");
	Image* image3 = loadBMP("textures\\textureAluNumber.bmp");
	Image* image4 = loadBMP("textures\\textureAluNumber2.bmp");
	textureMissile1 = loadTexture(image1);
	textureMissile2 = loadTexture(image2);
	textureM1 = loadTexture(image3);
	textureM2 = loadTexture(image4);

	currentTextureMissile = textureMissile1;
	currentTextureM = textureM1;

	delete image1;
	delete image2;
	delete image3;
	delete image4;
}

void MissileMunition::switchTexture()
{
	if(currentTextureMissile == textureMissile1)
		currentTextureMissile = textureMissile2;
	else
		currentTextureMissile = textureMissile1;
	if(currentTextureM == textureM1)
		currentTextureM = textureM2;
	else
		currentTextureM = textureM1;
}

void MissileMunition::setAngle(GLfloat a)
{
	if(!animated)
	{
		angle = a;
		angleAnimation = angle;
	}
	else
		angle = a;
}

void MissileMunition::display()
{
	// MISSILES
	for (int i=0; i!=2; ++i)
	{
		glPushMatrix();
			glTranslatef(0.0, elevation, 0.0);
			glTranslatef(x, 0, z);
			//glRotatef(180*angleAnimation/PI, 0, 1, 0);
			glRotatef(angleAnimation, 0, 1, 0);

			glBindTexture(GL_TEXTURE_2D, currentTextureMissile);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
			glColor3f(1.0f, 1.0f, 1.0f);

			gluQuadricNormals(quad, GLU_SMOOTH);   // Create Smooth Normals (better for light)
			gluQuadricTexture(quad, GL_TRUE);      // Create Texture Coords

			glTranslatef(pow(-1.0, i)*(lenght/2+lenght/6+sizeMissiles/2), 0, lenghtMissiles/2);
			glRotatef(180, 1.0, 0.0, 0.0);		
			glPushMatrix();
				glRotatef(180, 1.0, 0.0, 0.0);
				glColor3f(0,0,0);
				glutSolidCone(sizeMissiles, lenghtMissiles/4, 10, 30);
			glPopMatrix();
			glPushMatrix();
				gluCylinder (quad, sizeMissiles, sizeMissiles, lenghtMissiles, 10, 30);
			glPopMatrix();
			glPushMatrix();
				glTranslatef(0.0,0.0,lenghtMissiles);
				glNormal3f(0, 0, -1);
				gluDisk(quad, 0.0, sizeMissiles, 10,32);
			glPopMatrix();
			glPushMatrix();
				glBindTexture(GL_TEXTURE_2D, currentTextureM);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
				glColor3f(1.0f, 1.0f, 1.0f);

				glTranslatef(0.0, 0.0, lenghtMissiles - lenghtMissiles/6);
				glRotatef(90, 0.0, 1.0, 0.0);
				glPushMatrix();
					glDisable(GL_CULL_FACE);
					glColor3f(0.4, 0.4, 0.4);
					glBegin(GL_QUADS);
						glNormal3f(0,0,1);
						glTexCoord2f(0.0f, 0.0f); glVertex3f(0.0, 0.0, 0.0);
						glTexCoord2f(0.5f, 0.0f); glVertex3f(-lenghtMissiles/3, 0.0, 0.0);
						glTexCoord2f(0.75f, 0.25f); glVertex3f(-(lenghtMissiles/6 + lenghtMissiles/3), lenghtMissiles/6, 0.0);
						glTexCoord2f(0.25f, 0.25f); glVertex3f(-lenghtMissiles/6, lenghtMissiles/6, 0.0);
							
						glNormal3f(0,0,-1);
						glTexCoord2f(0.0f, 0.0f); glVertex3f(0.0, 0.0, 0.0);
						glTexCoord2f(0.5f, 0.0f); glVertex3f(-lenghtMissiles/3, 0.0, 0.0);
						glTexCoord2f(0.75f, 0.25f); glVertex3f(-(lenghtMissiles/6 + lenghtMissiles/3), -lenghtMissiles/6, 0.0);
						glTexCoord2f(0.25f, 0.25f); glVertex3f(-lenghtMissiles/6, -lenghtMissiles/6, 0.0);
							
						glNormal3f(0,-1,0);
						glTexCoord2f(0.0f, 0.0f); glVertex3f(0.0, 0.0, 0.0);
						glTexCoord2f(0.5f, 0.0f); glVertex3f(-lenghtMissiles/3, 0.0, 0.0);
						glTexCoord2f(0.75f, 0.25f); glVertex3f(-(lenghtMissiles/6 + lenghtMissiles/3), 0.0, lenghtMissiles/6);
						glTexCoord2f(0.25f, 0.25f); glVertex3f(-lenghtMissiles/6, 0.0, lenghtMissiles/6);
							
						glNormal3f(0,1,0);
						glTexCoord2f(0.0f, 0.0f); glVertex3f(0.0, 0.0, 0.0);
						glTexCoord2f(0.5f, 0.0f); glVertex3f(-lenghtMissiles/3, 0.0, 0.0);
						glTexCoord2f(0.75f, 0.25f); glVertex3f(-(lenghtMissiles/6 + lenghtMissiles/3), 0.0, -lenghtMissiles/6);
						glTexCoord2f(0.25f, 0.25f); glVertex3f(-lenghtMissiles/6, 0.0, -lenghtMissiles/6);
					glEnd();
					glEnable(GL_CULL_FACE);
				glPopMatrix();
			glPopMatrix();
		glPopMatrix();
	}
}

void MissileMunition::animate()
{
	if(animated)
	{
		/*if(angleAnimation == 0)
		{
			++oldZ;
		}
		else if(angleAnimation == 90)
		{
			++oldX;
		}
		else if(angleAnimation == 180)
		{
			--oldZ;
		}
		else
		{

		}*/
		oldX += sin(PI*angleAnimation/180);
		oldZ += cos(PI*angleAnimation/180);
		
		int idEnnemy = map->move(myX + sin(PI*angleAnimation/180), y, myZ + cos(PI*angleAnimation/180));
		if (!idEnnemy)
		{
			x = oldX;
			z = oldZ;
			y = elevation;
			myX += sin(PI*angleAnimation/180);
			myZ += cos(PI*angleAnimation/180);
		}
		else if(idEnnemy < 0)
		{
			reset();
		}
		else
		{
			collisionID = idEnnemy;
			crash = true;
			reset();
		}
	}
}

void MissileMunition::reset()
{
	oldX = 0;
	oldZ = 0;
	x = 0;
	z = 0;
	myX = xPos;
	myY = yPos;
	myZ = zPos;
	angleAnimation = angle;
	animated = false;
}

MissileMunition::~MissileMunition(void)
{
	gluDeleteQuadric(quad);
}
